<template>
    <el-container class="box">
        <!-- 头部 logo文字 +退出按钮 -->
        <el-header>
            <div class="logo">
                <img src="../assets/logo.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="tc">退出</el-button>
        </el-header>
        <el-container class="content">
            <!-- 侧边栏 导航 -->
            <!-- width灵活改变宽度 -->
            <el-aside :width="!flg ? '200px' : '64px'">
                <!-- 修改宽度 -->
                <div class="toggle_button" @click="flg = !flg">|||</div>
                <!-- 
                    导航
                    router 启用router模式,跳转到绑定的index上
                    是否可以收缩+动画
                    unique-opened 只保持一个菜单展开
                    背景颜色 文字颜色 选中颜色
                 -->
                <el-menu :router="true" :collapse="flg" :collapse-transition="false" :unique-opened=true
                    background-color="#333744" text-color="#fff" active-text-color="#409eff"
                    :default-active="$route.path == '/goods/add' ? '/goods' : $route.path">
                    <!-- 
                        一级菜单
                        循环菜单
                        模板是图片+文字
                        二级路由嵌套循环
                        index要绑定路由路径
                     -->
                    <el-submenu :index="item.path" v-for="(item, index) in asideList" :key="item.id">
                        <template slot="title">
                            <i :class="'iconfont ' + iconImg[index]"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + item2.path" v-for="(item2) in item.children" :key="item2.id">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{ item2.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主体 二级路由 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            asideList: [],
            iconImg: ['icon-yonghu', 'icon-quanxian', 'icon-shangpin', 'icon-dingdan', 'icon-tongji'],
            flg: false,
        }
    },
    methods: {
        tc() {
            //删除数据
            sessionStorage.removeItem('token')
            //跳转到login
            this.$router.push({ path: '/login' })
        },
        async init() {
            //获取列表数据
            let { data: res } = await this.$http.get("menus");
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            //
            this.asideList = res.data;
        }
    },
    mounted() {
        this.init();
    }
}
</script>

<style lang="scss" scoped>
.el-header {
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
}

.el-aside {
    background-color: #333744;
}

.el-main {
    background-color: #eaedf1;
}

.box {
    height: 100%;
}

.content {
    height: calc(100% - 60px);
}

.logo {
    display: flex;
    align-items: center;

    img {
        width: 50px;
        height: 50px;
    }

    span {
        margin-left: 15px;
        font-size: 20px;
        color: #fff;
    }
}

.iconfont {
    margin-right: 10px;
}

.el-menu {
    border-right: none;
}

.toggle_button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>